<!-- 登录页面 -->
<template>
    <div id="login_container">
        <div class="login-box">
            <div class="login-header">
                <h2>管理员登录</h2>
                <p>欢迎回来，请登录您的账户</p>
            </div>
            <!-- 登录表单 -->
            <div class="login-form">
                <!--  -->
                <div class="form-item">
                    <div class="item-icon">
                        <img style="width: 30px;" src="../public/username.png" alt="" srcset="">
                    </div>
                    <input type="text" v-model="loginForm.userName" placeholder="请输入用户名" @keyup.enter="handleLogin">
                </div>
                <div class="form-item">
                    <div class="item-icon">
                        <img style="width: 30px;" src="../public/password.png" alt="" srcset="">
                    </div>
                    <input :type="showPassword ? 'text' : 'password'" v-model="loginForm.password" placeholder="请输入密码"
                        @keyup.enter="handleLogin">
                    <div class="password-eye" @click="showPassword = !showPassword">
                        <img v-if="showPassword" style="width: 30px;" src="../public/eye_show.png" alt="" srcset="">
                        <img v-else style="width: 30px;" src="../public/eye_hide.png" alt="" srcset="">
                    </div>
                </div>

                <div class="form-item remember-row">
                    <label>
                        <input type="checkbox" v-model="rememberMe">
                        <span>记住我</span>
                    </label>
                    <a href="javascript:;" class="forget-pwd">忘记密码?</a>
                </div>
                <div class="form-item">
                    <button class="login-btn" :disabled="loginLoading" @click="handleLogin">
                        <span v-if="!loginLoading">登 录</span>
                        <span v-else>登录中...</span>
                    </button>
                </div>
                <div class="login-error" v-if="loginError">
                    {{ loginError }}
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { request } from '/public/request'; // 引入封装好的axios请求

const router = useRouter();
const showPassword = ref(false);
const loginLoading = ref(false);
const loginError = ref('');
const rememberMe = ref(false);

const loginForm = reactive({
    userName: '',
    password: ''
});

onMounted(() => {
    // 检查本地存储中是否有保存的用户名
    const savedUserName = localStorage.getItem('adminUserName');
    if (savedUserName) {
        loginForm.userName = savedUserName;
        rememberMe.value = true;
    }
});

const handleLogin = async () => {
    // 表单验证
    if (!loginForm.userName.trim()) {
        loginError.value = '请输入用户名';
        return;
    }
    if (!loginForm.password.trim()) {
        loginError.value = '请输入密码';
        return;
    }

    try {
        loginLoading.value = true;
        loginError.value = '';

        const res = await request({
            url: '/user/adminLogin',
            method: 'post',
            data: loginForm
        });

        if(res.data.code == 0){
            loginError.value = res.data.msg || '登录失败，请重试';
            return;
        }

        console.log("登录结果", res);
        
        // 保存token到localStorage
        localStorage.setItem('token', res.headers.authorization);

        // 如果选择"记住我"，保存用户名
        if (rememberMe.value) {
            localStorage.setItem('adminUserName', loginForm.userName);
        } else {
            localStorage.removeItem('adminUserName');
        }

        // 保存用户信息
        localStorage.setItem('userInfo', JSON.stringify(res.data.data));

        // 登录成功，跳转到首页
        setTimeout(() => {
            window.location.href = '/'; 
        }, 300);

    } catch (error) {
        console.error('登录失败:', error);
        if (error.response) {
            loginError.value = error.response.data.msg || '登录失败，请重试';
        } else {
            loginError.value = '网络错误，请检查您的网络连接';
        }
    } finally {
        loginLoading.value = false;
    }
};
</script>

<style scoped>
#login_container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    width: 100vw;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    overflow: hidden;
    box-sizing: border-box;
}

.login-box {
    width: 500px;
    padding: 40px;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
    box-sizing: border-box;

}

.login-box:hover {
    transform: translateY(-5px);
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
}

.login-header {
    text-align: center;
    margin-bottom: 30px;
}

.login-header h2 {
    font-size: 28px;
    color: #333;
    margin-bottom: 10px;
    font-weight: 600;
}

.login-header p {
    color: #888;
    font-size: 16px;
}

.form-item {
    position: relative;
    margin-bottom: 20px;
}

.form-item input[type="text"],
.form-item input[type="password"] {
    width: 100%;
    box-sizing: border-box;
    height: 50px;
    padding: 0 15px 0 50px;
    border: 1px solid #e0e0e0;
    border-radius: 5px;
    font-size: 16px;
    transition: all 0.3s;
    outline: none;
}

.form-item input:focus {
    border-color: #667eea;
    box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
}

.item-icon {
    position: absolute;
    left: 15px;
    top: 50%;
    transform: translateY(-50%);
    color: #888;
}

.password-eye {
    position: absolute;
    right: 15px;
    top: 50%;
    transform: translateY(-50%);
    cursor: pointer;
    color: #888;
}

.remember-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
}

.remember-row label {
    display: flex;
    align-items: center;
    cursor: pointer;
}

.remember-row input[type="checkbox"] {
    margin-right: 8px;
}

.forget-pwd {
    color: #667eea;
    text-decoration: none;
}

.forget-pwd:hover {
    text-decoration: underline;
}

.login-btn {
    width: 100%;
    height: 50px;
    background: linear-gradient(to right, #667eea, #764ba2);
    border: none;
    border-radius: 5px;
    color: white;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
}

.login-btn:hover {
    opacity: 0.9;
    transform: translateY(-2px);
}

.login-btn:disabled {
    background: #cccccc;
    cursor: not-allowed;
    transform: none;
}

.login-error {
    color: #ff4d4f;
    font-size: 14px;
    text-align: center;
    margin-top: 10px;
}

/* 添加一些图标字体的样式，您需要在项目中引入相应的图标库 */
@font-face {
    font-family: 'iconfont';
    src: url('//at.alicdn.com/t/font_1234567_abcdefg.woff') format('woff');
}

.iconfont {
    font-family: "iconfont" !important;
    font-size: 18px;
    font-style: normal;
}

.icon-user:before {
    content: "\e7ae";
}

.icon-lock:before {
    content: "\e7c9";
}

.icon-eye:before {
    content: "\e78f";
}

.icon-eye-close:before {
    content: "\e790";
}
</style>